

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>LLVM Branch Weight Metadata &mdash; LLVM 3.2 documentation</title>
    
    <link rel="stylesheet" href="_static/llvm-theme.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '',
        VERSION:     '3.2',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/underscore.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <link rel="top" title="LLVM 3.2 documentation" href="index.html" />
    <link rel="up" title="Subsystem Documentation" href="subsystems.html" />
    <link rel="next" title="LLVM bugpoint tool: design and usage" href="Bugpoint.html" />
    <link rel="prev" title="LLVM Bitcode File Format" href="BitCodeFormat.html" />
<style type="text/css">
  table.right { float: right; margin-left: 20px; }
  table.right td { border: 1px solid #ccc; }
</style>

  </head>
  <body>
<div class="logo">
  <a href="index.html">
    <img src="_static/logo.png"
         alt="LLVM Logo" width="250" height="88"/></a>
</div>

    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="Bugpoint.html" title="LLVM bugpoint tool: design and usage"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="BitCodeFormat.html" title="LLVM Bitcode File Format"
             accesskey="P">previous</a> |</li>
  <li><a href="http://llvm.org/">LLVM Home</a>&nbsp;|&nbsp;</li>
  <li><a href="index.html">Documentation</a>&raquo;</li>

          <li><a href="subsystems.html" accesskey="U">Subsystem Documentation</a> &raquo;</li> 
      </ul>
    </div>

      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3><a href="index.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">LLVM Branch Weight Metadata</a><ul>
<li><a class="reference internal" href="#introduction">Introduction</a></li>
<li><a class="reference internal" href="#supported-instructions">Supported Instructions</a><ul>
<li><a class="reference internal" href="#branchinst"><tt class="docutils literal"><span class="pre">BranchInst</span></tt></a></li>
<li><a class="reference internal" href="#switchinst"><tt class="docutils literal"><span class="pre">SwitchInst</span></tt></a></li>
<li><a class="reference internal" href="#indirectbrinst"><tt class="docutils literal"><span class="pre">IndirectBrInst</span></tt></a></li>
<li><a class="reference internal" href="#other">Other</a></li>
</ul>
</li>
<li><a class="reference internal" href="#built-in-expect-instructions">Built-in <tt class="docutils literal"><span class="pre">expect</span></tt> Instructions</a><ul>
<li><a class="reference internal" href="#if-statement"><tt class="docutils literal"><span class="pre">if</span></tt> statement</a></li>
<li><a class="reference internal" href="#switch-statement"><tt class="docutils literal"><span class="pre">switch</span></tt> statement</a></li>
</ul>
</li>
<li><a class="reference internal" href="#cfg-modifications">CFG Modifications</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="BitCodeFormat.html"
                        title="previous chapter">LLVM Bitcode File Format</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="Bugpoint.html"
                        title="next chapter">LLVM bugpoint tool: design and usage</a></p>
  <h3>This Page</h3>
  <ul class="this-page-menu">
    <li><a href="_sources/BranchWeightMetadata.txt"
           rel="nofollow">Show Source</a></li>
  </ul>
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="llvm-branch-weight-metadata">
<span id="branch-weight"></span><h1>LLVM Branch Weight Metadata<a class="headerlink" href="#llvm-branch-weight-metadata" title="Permalink to this headline">¶</a></h1>
<div class="contents local topic" id="contents">
<ul class="simple">
<li><a class="reference internal" href="#introduction" id="id1">Introduction</a></li>
<li><a class="reference internal" href="#supported-instructions" id="id2">Supported Instructions</a><ul>
<li><a class="reference internal" href="#branchinst" id="id3"><tt class="docutils literal"><span class="pre">BranchInst</span></tt></a></li>
<li><a class="reference internal" href="#switchinst" id="id4"><tt class="docutils literal"><span class="pre">SwitchInst</span></tt></a></li>
<li><a class="reference internal" href="#indirectbrinst" id="id5"><tt class="docutils literal"><span class="pre">IndirectBrInst</span></tt></a></li>
<li><a class="reference internal" href="#other" id="id6">Other</a></li>
</ul>
</li>
<li><a class="reference internal" href="#built-in-expect-instructions" id="id7">Built-in <tt class="docutils literal"><span class="pre">expect</span></tt> Instructions</a><ul>
<li><a class="reference internal" href="#if-statement" id="id8"><tt class="docutils literal"><span class="pre">if</span></tt> statement</a></li>
<li><a class="reference internal" href="#switch-statement" id="id9"><tt class="docutils literal"><span class="pre">switch</span></tt> statement</a></li>
</ul>
</li>
<li><a class="reference internal" href="#cfg-modifications" id="id10">CFG Modifications</a></li>
</ul>
</div>
<div class="section" id="introduction">
<h2><a class="toc-backref" href="#id1">Introduction</a><a class="headerlink" href="#introduction" title="Permalink to this headline">¶</a></h2>
<p>Branch Weight Metadata represents branch weights as its likeliness to be
taken. Metadata is assigned to the <tt class="docutils literal"><span class="pre">TerminatorInst</span></tt> as a <tt class="docutils literal"><span class="pre">MDNode</span></tt> of the
<tt class="docutils literal"><span class="pre">MD_prof</span></tt> kind. The first operator is always a <tt class="docutils literal"><span class="pre">MDString</span></tt> node with the
string &#8220;branch_weights&#8221;. Number of operators depends on the terminator type.</p>
<p>Branch weights might be fetch from the profiling file, or generated based on
<a class="reference internal" href="#builtin-expect">__builtin_expect</a> instruction.</p>
<p>All weights are represented as an unsigned 32-bit values, where higher value
indicates greater chance to be taken.</p>
</div>
<div class="section" id="supported-instructions">
<h2><a class="toc-backref" href="#id2">Supported Instructions</a><a class="headerlink" href="#supported-instructions" title="Permalink to this headline">¶</a></h2>
<div class="section" id="branchinst">
<h3><a class="toc-backref" href="#id3"><tt class="docutils literal"><span class="pre">BranchInst</span></tt></a><a class="headerlink" href="#branchinst" title="Permalink to this headline">¶</a></h3>
<p>Metadata is only assign to the conditional branches. There are two extra
operarands, for the true and the false branch.</p>
<div class="highlight-llvm"><pre>!0 = metadata !{
  metadata !"branch_weights",
  i32 &lt;TRUE_BRANCH_WEIGHT&gt;,
  i32 &lt;FALSE_BRANCH_WEIGHT&gt;
}</pre>
</div>
</div>
<div class="section" id="switchinst">
<h3><a class="toc-backref" href="#id4"><tt class="docutils literal"><span class="pre">SwitchInst</span></tt></a><a class="headerlink" href="#switchinst" title="Permalink to this headline">¶</a></h3>
<p>Branch weights are assign to every case (including <tt class="docutils literal"><span class="pre">default</span></tt> case which is
always case #0).</p>
<div class="highlight-llvm"><pre>!0 = metadata !{
  metadata !"branch_weights",
  i32 &lt;DEFAULT_BRANCH_WEIGHT&gt;
  [ , i32 &lt;CASE_BRANCH_WEIGHT&gt; ... ]
}</pre>
</div>
</div>
<div class="section" id="indirectbrinst">
<h3><a class="toc-backref" href="#id5"><tt class="docutils literal"><span class="pre">IndirectBrInst</span></tt></a><a class="headerlink" href="#indirectbrinst" title="Permalink to this headline">¶</a></h3>
<p>Branch weights are assign to every destination.</p>
<div class="highlight-llvm"><pre>!0 = metadata !{
  metadata !"branch_weights",
  i32 &lt;LABEL_BRANCH_WEIGHT&gt;
  [ , i32 &lt;LABEL_BRANCH_WEIGHT&gt; ... ]
}</pre>
</div>
</div>
<div class="section" id="other">
<h3><a class="toc-backref" href="#id6">Other</a><a class="headerlink" href="#other" title="Permalink to this headline">¶</a></h3>
<p>Other terminator instructions are not allowed to contain Branch Weight Metadata.</p>
</div>
</div>
<div class="section" id="built-in-expect-instructions">
<span id="builtin-expect"></span><h2><a class="toc-backref" href="#id7">Built-in <tt class="docutils literal"><span class="pre">expect</span></tt> Instructions</a><a class="headerlink" href="#built-in-expect-instructions" title="Permalink to this headline">¶</a></h2>
<p><tt class="docutils literal"><span class="pre">__builtin_expect(long</span> <span class="pre">exp,</span> <span class="pre">long</span> <span class="pre">c)</span></tt> instruction provides branch prediction
information. The return value is the value of <tt class="docutils literal"><span class="pre">exp</span></tt>.</p>
<p>It is especially useful in conditional statements. Currently Clang supports two
conditional statements:</p>
<div class="section" id="if-statement">
<h3><a class="toc-backref" href="#id8"><tt class="docutils literal"><span class="pre">if</span></tt> statement</a><a class="headerlink" href="#if-statement" title="Permalink to this headline">¶</a></h3>
<p>The <tt class="docutils literal"><span class="pre">exp</span></tt> parameter is the condition. The <tt class="docutils literal"><span class="pre">c</span></tt> parameter is the expected
comparison value. If it is equal to 1 (true), the condition is likely to be
true, in other case condition is likely to be false. For example:</p>
<div class="highlight-c++"><div class="highlight"><pre><span class="k">if</span> <span class="p">(</span><span class="n">__builtin_expect</span><span class="p">(</span><span class="n">x</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">))</span> <span class="p">{</span>
  <span class="c1">// This block is likely to be taken.</span>
<span class="p">}</span>
</pre></div>
</div>
</div>
<div class="section" id="switch-statement">
<h3><a class="toc-backref" href="#id9"><tt class="docutils literal"><span class="pre">switch</span></tt> statement</a><a class="headerlink" href="#switch-statement" title="Permalink to this headline">¶</a></h3>
<p>The <tt class="docutils literal"><span class="pre">exp</span></tt> parameter is the value. The <tt class="docutils literal"><span class="pre">c</span></tt> parameter is the expected
value. If the expected value doesn&#8217;t show on the cases list, the <tt class="docutils literal"><span class="pre">default</span></tt>
case is assumed to be likely taken.</p>
<div class="highlight-c++"><div class="highlight"><pre><span class="k">switch</span> <span class="p">(</span><span class="n">__builtin_expect</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="mi">5</span><span class="p">))</span> <span class="p">{</span>
<span class="k">default</span><span class="o">:</span> <span class="k">break</span><span class="p">;</span>
<span class="k">case</span> <span class="mi">0</span><span class="o">:</span>  <span class="c1">// ...</span>
<span class="k">case</span> <span class="mi">3</span><span class="o">:</span>  <span class="c1">// ...</span>
<span class="k">case</span> <span class="mi">5</span><span class="o">:</span>  <span class="c1">// This case is likely to be taken.</span>
<span class="p">}</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="cfg-modifications">
<h2><a class="toc-backref" href="#id10">CFG Modifications</a><a class="headerlink" href="#cfg-modifications" title="Permalink to this headline">¶</a></h2>
<p>Branch Weight Metatada is not proof against CFG changes. If terminator operands&#8217;
are changed some action should be taken. In other case some misoptimizations may
occur due to incorrent branch prediction information.</p>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="Bugpoint.html" title="LLVM bugpoint tool: design and usage"
             >next</a> |</li>
        <li class="right" >
          <a href="BitCodeFormat.html" title="LLVM Bitcode File Format"
             >previous</a> |</li>
  <li><a href="http://llvm.org/">LLVM Home</a>&nbsp;|&nbsp;</li>
  <li><a href="index.html">Documentation</a>&raquo;</li>

          <li><a href="subsystems.html" >Subsystem Documentation</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2012, LLVM Project.
      Last updated on 2012-12-21.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3.
    </div>
  </body>
</html>